/*
 *  ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
 *  m_scanhost.c: Display a users with agree host.
 *
 *  Copyright (C) 2002 by Ilya Shtift.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *  USA
 *
 *  $Id: $
 *
 */

#include "stdinc.h"
#include "handlers.h"
#include "client.h"
#include "common.h"
#include "hash.h"
#include "ircd.h"
#include "numeric.h"
#include "s_log.h"
#include "s_serv.h"
#include "s_conf.h"
#include "send.h"
#include "whowas.h"
#include "irc_string.h"
#include "sprintf_irc.h"
#include "msg.h"
#include "parse.h"
#include "channel_mode.h"    /* needed only for split_nuh() */
#include "modules.h"

static void mo_scanhost(struct Client *, struct Client *, int, char *[]);

struct Message scanhost_msgtab = {
  "SCANHOST", 0, 0, 2, 0, MFLG_SLOW, 0,
  { m_unregistered, m_ignore, m_ignore, m_ignore, mo_scanhost, m_ignore }
};

#ifndef STATIC_MODULES
void
_modinit(void)
{
  mod_add_cmd(&scanhost_msgtab);
}

void
_moddeinit(void)
{
  mod_del_cmd(&scanhost_msgtab);
}

const char *_version = "$Revision: 1 $";
#endif

/* mo_scanhost()
 * Created May 5, 2003
 * common (Ilya Shtift) ishtift@tagil.svrw.ru
 *
 *      parv[0] = sender prefix
 *      parv[1] = host
 *	parv[2] = [limit], default 100
 */
static void
mo_scanhost(struct Client *client_p, struct Client *source_p,
            int parc, char *parv[])
{
  dlink_node *ptr = NULL, *ptr_next = NULL;
  struct Client *target_p = NULL;
  char nick[NICKLEN + 1];
  char user[USERLEN + 1];
  char host[HOSTLEN + 1];
  struct split_nuh_item nuh;
  unsigned int count = 0, maxmatches = 100, broken = FALSE;

  nuh.nuhmask  = parv[1];
  nuh.nickptr  = nick;
  nuh.userptr  = user;
  nuh.hostptr  = host;

  nuh.nicksize = sizeof(nick);
  nuh.usersize = sizeof(user);
  nuh.hostsize = sizeof(host);

  split_nuh(&nuh);

  if (parc >= 3)
    maxmatches = atoi(parv[2]);

  DLINK_FOREACH_SAFE(ptr, ptr_next, global_client_list.head)
  {
    target_p = ptr->data;

    if (!IsClient(target_p) || (source_p == target_p))
      continue;

    if (match(nick, target_p->name) &&
        match(user, target_p->username) &&
        match(host, target_p->host))
    {
      count++;
      sendto_one(source_p, ":%s NOTICE %s :%s!%s@%s at %s", me.name, parv[0],
                 target_p->name, target_p->username, target_p->host, target_p->servptr->name);
      
      if (count >= maxmatches)
      {
        broken = TRUE;
	break;
      }
    }
    
    if (broken)
      break;
  }
  
  sendto_one(source_p, ":%s NOTICE %s :%d clients match", me.name, parv[0], count);
}
